<template>
	<view class="mainBox">
		<waterfall :pageSize="pageSize" :pageNum="pageNum" :columsp="columsp"></waterfall>
	</view>
</template>

<script>
	import {
		selectTxtNovel,
		addUserTask
	} from "@/api/index.js"
	import {
		mapMutations,
		mapState,
		mapActions
	} from "vuex"
	import fileApi from "@/uni_modules/hbh-fileIo/js_sdk/index.js"
	export default {
		name: "txtList",
		data() {
			return {
				leftHeight: 0,
				rightHeight: 1,
				list: [],
				limitSize: 20,
				total: 0,
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				flag: false,
				isFrist: true,
				ids: "",
				requestFlag: true,
				timer: null,
				triggered: true,
				markFlag: false,
				count: 0,
				loadingShow: false,
				loadSucess: false,
				pageSize: 20,
				pageNum: 1,
				columsp: 2, //分割几列
			};
		},
		components: {

		},
		computed: {
			...mapState("home", ["downTxtLoad", "homeIsRush"]),
			...mapState("txtDownTaskStore", ["downTaskList"])
		},
		mounted() {
			// this.getTxtNovel(true)
		},
		methods: {
			...mapActions(["txtDownTaskStore/add"]),
			refresherrefresh(e) {
				this.triggered = true;
				this.getTxtNovel(true)
				this.markFlag = true
				setTimeout(() => {
					this.triggered = false;
					this.markFlag = false
				}, 1000)
			},
			lower(e) {
				this.getTxtNovel(false)
			},
			scroll(e) {
				this.loadSucess = false
				this.old.scrollTop = e.detail.scrollTop
			},
			goTop(e) {
				// 解决view层不同步的问题
				this.scrollTop = this.old.scrollTop
				this.$nextTick(()=> {
					this.scrollTop = 0
				});
			},
			getTxtNovel(val) {
				if (val) {
					this.ids = ""
					selectTxtNovel({
						limitSize: this.limitSize,
						ids: this.ids
					}).then((res) => {
						if (res.code == 200) {
							this.total = res.total
							this.ids = (res.data.length > 0 && this.ids.length > 0) ? (this.ids +
								',' + res.data.map(item => item.id)
								.join(
									",")) : (res.data.map(item => item.id).join(","))
							res.data.forEach(async (item, index) => {
								item.isdown = false //用户是否在下载过
								item.isDecompression = false //标记小说下载完后是否解压分割了
								item.downStatus = 3 //默认未开始下载
								item.isStop = false //是否暂停下载
								item.downingSize = 0 //初始下载量大小
								item.totalSize = 0 //初始总大小
								item.process = 0 //初始下载进度
								item.isStart = false //是否开始下载
								item.downIcon = false //false可下载
								item.isSign = false //下载过本地被删除
								item.id = 'hbh' + (this.list.length + index)
								let rame = parseInt(Math.random() *
									200)
								item.ranmHeight = rame < 50 ? 100 :
									rame
								//检查本地小说与接口数组相同的数据并修改其对应状态
								const isExist = await fileApi.fileIsExist(
									`downloadTxt/${item.fileNameId}/${item.bookName+'.txt'}`
								)
								if (isExist) {
									item.downIcon = true
									item.isdown = true
								}

							})
							//检查本地存在的修改结果状态
							this.list = res.data
							this.$nextTick(() => {
								this.$refs.pop.init("init", this.list)
							})
						}
					}).catch((err) => {
						console.log(err)
					})
					return
				}
				if (!this.requestFlag) {
					return
				}
				this.$nextTick(() => {
					this.loadingShow = true
				})

				this.requestFlag = false
				setTimeout(() => {
					selectTxtNovel({
						limitSize: this.limitSize,
						ids: this.ids
					}).then((res) => {
						setTimeout(() => {
							this.requestFlag = true
						}, 1500)
						if (res.code == 200) {
							this.total = res.total
							if (res.data.length <= 0) {
								this.loadingShow = false
								this.loadSucess = true
								setTimeout(() => {
									this.loadSucess = false
								}, 1000)
								return
							}
							this.loadingShow = false
							this.loadSucess = false
							let idsTring = res.data.map(item => item.id).join(",")
							this.ids = (res.data.length > 0 && this.ids
								.length > 0) ? (this
								.ids + ',' + idsTring) : idsTring
							res.data.forEach(async (item, index) => {
								item.isdown =
									false //用户是否在下载过
								item.isDecompression =
									false //标记小说下载完后是否解压分割了
								item.downStatus =
									3 //默认未开始下载
								item.isStop =
									false //是否暂停下载
								item.downingSize =
									0 //初始下载量大小
								item.totalSize =
									0 //初始总大小
								item.process =
									0 //初始下载进度
								item.isStart =
									false //是否开始下载
								item.downIcon =
									false //false可下载
								item.isSign =
									false //下载过本地被删除
								item.id = 'hbh' + (this
									.list
									.length +
									index)
								let rame = parseInt(
									Math
									.random() *
									200)
								item.ranmHeight =
									rame < 50 ? 100 :
									rame
								//检查本地小说与接口数组相同的数据并修改其对应状态
								const isExist =
									await fileApi
									.fileIsExist(
										`downloadTxt/${item.fileNameId}/${item.bookName+'.txt'}`
									)
								if (isExist) {
									item.downIcon =
										true
									item.isdown =
										true
								}

							})
							this.$nextTick(() => {
								this.$refs.pop.addLoad(res.data)
							})

						}

					}).catch((err) => {
						uni.showToast({
							icon: "error",
							title: "执行异常!catch(err)"
						})
						this.$nextTick(() => {
							this.loadingShow = false
							this.loadSucess = false
						})
						setTimeout(() => {
							this.requestFlag = true
						}, 1500)
					})
				}, 1000)

			},
			handleTxt(item) {
				uni.navigateTo({
					url: `/pages/txtNovelContent/txtNovelContent?bookName= ${item.bookName}`
				})
			},
			async addList(item, index) {
				if (!item.downIcon) {
					item.downIcon = true
					this["txtDownTaskStore/add"](item)
				} else {
					uni.showToast({
						icon: "none",
						title: "已添加，下载完成后到离线书架阅读！"
					})
				}

			}
		}
	}
</script>

<style lang="scss" scoped>
	.mainBox {
		height: calc(100% - 200rpx);
		padding: 30rpx;
		box-sizing: border-box;
		background-color: #e7ecfa;
	}
</style>